package com.time.company.components.task;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.makeramen.roundedimageview.RoundedImageView;
import com.time.company.R;
import com.time.company.base.BaseRecAdapter;
import com.time.company.base.BaseViewHolder;
import com.time.company.servermodel.AppBanner;
import com.time.company.utils.ScreenUtils;
import com.time.company.webserver.helper.picasso.PicassoImageLoader;

import java.util.List;

/**
 * 任务 - banner
 * Created by Bing on 2017/11/9.
 * E-mail is tx16bing@163.com
 */

public class BannerAdapter extends BaseRecAdapter<AppBanner> {

    public BannerAdapter(Context mContext, List<AppBanner> list, int viewTypeCount) {
        super(mContext, list, viewTypeCount);
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = inflater.inflate(R.layout.item_task_banner, parent, false);
        BannerViewHolder recentViewHolder = new BannerViewHolder(convertView);
        convertView.setTag(recentViewHolder);
        return recentViewHolder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, final int position) {
        final AppBanner banner = getItem(position);
        BannerViewHolder bannerHolder = (BannerViewHolder) holder;
        if (!TextUtils.isEmpty(banner.getPicUrl())) {
            int height = ScreenUtils.dip2px(86);
            PicassoImageLoader.loadImageViewSize(mContext,
                    banner.getPicUrl(),
                    ScreenUtils.getDisplayWidth(),
                    height,
                    R.drawable.iv_task_banner_default,
                    R.drawable.iv_task_banner_default,
                    toString(), bannerHolder.rivPic);
        }

        if (position == getItemCount()-1){
            bannerHolder.divider.setVisibility(View.GONE);
        }else {
            bannerHolder.divider.setVisibility(View.VISIBLE);
        }

        bannerHolder.llParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != onItemClickListener){
                    if (TextUtils.isEmpty(banner.getActiveUrl())){
                        return;
                    }else {
                        onItemClickListener.onItemClick(v, position, banner);
                    }
                }
            }
        });

    }

    class BannerViewHolder extends BaseViewHolder {

        public LinearLayout llParent;
        public RoundedImageView rivPic;
        public View divider;

        public BannerViewHolder(View itemView) {
            super(itemView);
            llParent = getView(R.id.ll_recent_item_parent);
            rivPic = getView(R.id.riv_task_banner);
            divider = getView(R.id.v_divider);
        }
    }

}
